var myGrid;
var zTreeObj;
var cNode;
var setting = {
		check : {
			enable : false
		},
		async : {
			enable : true,
			url : "newscatalog/news-catalog!listbyparent",
			autoParam : [ "id=bean.id" ],
			otherParam : {}
		},
		callback : {
			onClick:zTreeOnClick
		}
	};
	var root = {
		name : "所有新闻分类",
		open : true,
		nocheck : false,
		id : "root",
		isParent : true
	};
function zTreeOnClick(event, treeId, treeNode) {
	 if(!treeNode.isParent) return;
	 var pms =  {};
		pms["searchMap['parentId']"]=treeNode.id;
		myGrid.jqGrid('setGridParam',{url:"newscatalog/news-catalog!datalist",postData:pms}).trigger("reloadGrid");
}
$(function(){
	
	$("#date").datepicker({"dateFormat":"yy-mm-dd"});	
	var zNodes = [ root ];
	zTreeObj = $.fn.zTree.init($("#resTree"), setting, zNodes);
	var nodes = zTreeObj.getNodes();
	zTreeObj.expandNode(nodes[0], null, false, false, true);
	
	$("#externalcheck").bind('click',function(){
		$("#beanhref").attr("disabled",!this.checked);
		$("#beanhref").toggle();
	});
	$("#saveBtn").bind("click",function(){		
		doGet("newscatalog/start");
		
	});
	
	myGrid = $("#list").jqGrid({
			url : "newscatalog/news-catalog!datalist",
			datatype : "json",
			mtype : 'GET',
			colNames : [ 
			   '名称',			   
			   '描述',
			   '外?',
			   '连接',
			   '管理'
			],
			colModel : [
			      
			      {
					name : 'name',
					index : 'name',
					width : 200			
				  },
				  {
						name : 'description',
						index : 'description',
						width : 200			
				  },
				  {
					  name:'external',
					  index:'external',
					  width:50,
					  formatter:checkFormatter
				  },
				  {
					  name:"href",
					  index:'href',
					  width:100,
					  formatter:hrefFormatter
				  },
				  {
					   name:'id',
					   index:'id',
					   width:50,
					   formatter:mgrFormatter
						   
				  }
			],
			pager : '#pager',
			rowNum : 10,
			rowList : [ 10, 20, 30 ],
			sortname : 'name',
			sortorder : 'asc',
			viewrecords : true,
			caption : "新闻分类列表",
			width : '550',
			height : '300',			
			jsonReader : {
				repeatitems : false,
				id : "0"
			}
		});	
	
	
	myGrid.jqGrid("navGrid", "#pager", {
		edit : false,
		add : false,
		del : false,
		search : false
	});
	myGrid.jqGrid("navButtonAdd", "#pager", {
		onClickButton : function() {
			doGet("newscatalog/start");
		},
		buttonicon : "ui-icon-newwin",
		caption : "添加",
		id : "newGroup"
	});
});

function checkFormatter(v,opt,row){
	var c = $("<input type='checkbox'></input>");
	c.attr("checked",v?true:false);
	c.attr("disabled",true);
	return $("<a></a>").append(c).html();
}
function mgrFormatter(v, opt, row) {
	var link = $("<a href=\"javascript:removeOne('" + v
			+ "','"+row['name']+"');\"><img style='border:none' src='" + sbase
			+ "images/removeme.gif'></a>");	
	return $("<p></p>").append(link).html();

}
function removeOne(id,name) {
	if(!confirm("确认要删除分类:"+name+" , 该分类删除后,其下发表的所有文章也会被全部删除?"))return;
	var url = "newscatalog/news-catalog!remove";
	var data = "bean.id=" + id;
	doSubmit(url, data, function() {
		myGrid.trigger("reloadGrid");
	});
}
function hrefFormatter(v,opt,row){
	var external = row['external'];
	var ta = "<textarea>"+v+"</textarea>";
	
	return external?ta:'-';
	
}